﻿using ReaderB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TS_XY_Train_Repaire.ToolSystem
{
    class CRfid
    {
        private byte[] EPC = new byte[5000];
        private int Totallen = 0;
        private int CardNum = 0;
        private int m_iPortIndex = -1;

        private int port;  // 串口号，根据实际情况赋值
        private byte fComAdr = 0xff;
        private byte fBaud = 5;  // 查看文档，该值为0-6

        ~CRfid()
        {
            if (m_iPortIndex != -1)
            {
                StaticClassReaderB.CloseSpecComPort(m_iPortIndex);  // 关闭串口
                m_iPortIndex = -1;
            }
        }

        private List<string> m_listRFID;

        public bool Init()
        {
            try
            {
                for (byte fBaud = 0; fBaud < 7; ++fBaud)
                {
                    int openresult = StaticClassReaderB.AutoOpenComPort(ref port, ref fComAdr, fBaud, ref m_iPortIndex);
                    if (openresult == 0)
                    {
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return false;
        }

        public bool UnInit()
        {
            try
            {
                if (m_iPortIndex > 0)
                {
                    StaticClassReaderB.CloseSpecComPort(m_iPortIndex);  // 关闭串口
                    m_iPortIndex = -1;
                }

                return true;
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return true;
        }

        public bool GetRFID(ref List<string> listRFID)
        {
            if (m_iPortIndex == -1)
            {
                return false;
            }

            try
            {
                byte ConAddr = 0;
                byte AdrTID = 0;
                byte LenTID = 0;
                byte TIDFlag = 0;
                int fCmdRet = StaticClassReaderB.Inventory_G2(ref ConAddr, AdrTID, LenTID, TIDFlag, EPC, ref Totallen, ref CardNum, m_iPortIndex);
                if ((fCmdRet == 1) | (fCmdRet == 2) | (fCmdRet == 3) | (fCmdRet == 4) | (fCmdRet == 0xFB))
                {
                    byte[] daw = new byte[Totallen];
                    Array.Copy(EPC, daw, Totallen);
                    String temps = ByteArrayToHexString(daw);  //转换为十六进制字符串，这就是相当于唯一标识符

                    int iPos = 0;
                    for (int CardIndex = 0; CardIndex < CardNum; CardIndex++)
                    {
                        int EPClen = daw[iPos];
                        string sEPC = temps.Substring(iPos * 2 + 2, EPClen * 2);
                        iPos = iPos + EPClen + 1;
                        if (sEPC.Length != EPClen * 2)
                            return false;

                        listRFID.Add(sEPC);
                    }
                }

                return true;
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return false;
        }

        private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));

            return sb.ToString().ToUpper();
        }
    }
}
